var util = require('../../utils/util.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0, // tab切换 
    isHiddenMes: true,
    unorderInfo: {},//1待接单
    orderingInfo: {},//2进行中
    safeorderInfo: {},//维权中
    orderedInfo: {},//3已完成，4已取消，5已拒单
    orderAssessInfo:{},//待评价
    orderedInfoHide:[],
    cont: "",
    showSafe:false,
    userInfo: '',
    assessInfo:{},
    avatarUrl: '/images/small_avatar.png',
    nickname: '',
    starImgList: [1, 2, 3, 4, 5],
    replyPhone:'17095057022'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    util.post(app.api.getUnOrderByStatus, { openid: app.globalData.openid, status: 0}, function (res)   {
      console.log(res.data);
      that.setData({
        unorderInfo: res.data,
      });
    });
    util.post(app.api.getOrderingByStatus, { openid: app.globalData.openid, status: 1 }, function (res) {
      console.log(res.data);
      that.setData({
        orderingInfo: res.data,
      });
    });

    util.post(app.api.getAssessOrderByStatus, { openid: app.globalData.openid, status: 3 }, function (res) {
      that.setData({
        orderAssessInfo: res.data,
      });
    });

    util.post(app.api.getOrderedByStatus, { openid: app.globalData.openid, status: 6 }, function(res) {
      var hideList=new Array();
     
      for (var i = 0; i < res.data.length; i++){
        hideList.push(false);
      }
      that.setData({
        orderedInfoHide: hideList,
        orderedInfo: res.data,
      });

    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight
        });
      }
    });
    showSafe: (options.showSafe == "false" ? false : true);
    that.setData({
      userInfo: app.getUserInfo(),
    });
    that.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl != null ? app.globalData.userInfo.avatarUrl : '/images/small_avatar.png'
    });
  },
  resetUnOrder:function(){
    var that = this;
    util.post(app.api.getUnOrderByStatus, { openid: app.globalData.openid,status:0},function(res){
      if(res.code=="200"){
        that.setData({
          unorderInfo: res.data
        });
      }
    });
  },
  resetOrderingInfo: function () {
    var that = this;
    util.post(app.api.getOrderingByStatus, { openid: app.globalData.openid, status:1 }, function (res) {
      if (res.code == "200") {
        that.setData({
          orderingInfo: res.data
        });
      }
    });
  },
  resetOrderAssessInfo: function () {
    var that = this;
    util.post(app.api.getAssessOrderByStatus, { openid: app.globalData.openid, status: 3 }, function (res) {
      if (res.code == "200") {
        that.setData({
          orderAssessInfo: res.data
        });
      }
    });
  },
  resetSafeOrderInfo: function () {
    var that = this;
    util.post(app.api.getSafeOrderByStatus, { openid: app.globalData.openid,status:2 }, function (res) {
      if (res.code == "200") {
        that.setData({
          safeorderInfo: res.data
        });
      }
    });
  },
  resetOrderedInfo: function () {
    var that = this;
    util.post(app.api.getOrderedByStatus, { openid: app.globalData.openid,  status:6 }, function (res) {
      if (res.code == "200") {
        that.setData({
          orderedInfo: res.data
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    // that.resetUnOrder();
    // that.resetOrderingInfo();
    that.resetOrderAssessInfo();
    that.resetOrderedInfo();
  },

  //滑动切换tab
  bindChangeTab: function (e) {
    this.setData({ currentTab: e.detail.current });
  },
  
  //点击tab切换
  swichNav: function (e) {
    var that = this ;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
   
  },
  //申请维权
  applyTap: function (event) {
    var that = this;
    var orderId = event.currentTarget.dataset.id;
    var orderMoney = event.currentTarget.dataset.ordermoney;
    var hunterId=event.currentTarget.dataset.hunterid;
    var orderdesc=event.currentTarget.dataset.orderdesc;
    if (orderMoney == null) { orderMoney = 0; }
    if (hunterId == null) { hunterId = 0; }
      wx.showModal({
        title: '申请维权',
        content: '请确认要申请维权该订单？',
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '确认',
        confirmColor: '#4990E2',
        success: function (res) {
          if (res.confirm) {
            app.loading();
            util.post(app.api.createSafesales, {
              openid: app.globalData.openid,
              nickName: that.data.userInfo.nickName,
              status: 2,
              orderId:orderId, 
              hunterId: hunterId,
              orderdescIds: "￥"+orderMoney+"/"+orderdesc,
              afterContent: that.data.cont,
              afterMoney: orderMoney}, function (res) {
              if (res.code == "200") {
                // 维权已提交
                wx.showToast({
                  title: '维权已提交',
                  icon: 'success',
                  duration: 2000
                })
                that.resetOrderedInfo();
              }
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    //申请退款
  refundMoneyTap: function (event) {
    var that = this;
    var formId = event.detail.formId;
    var orderId = event.currentTarget.dataset.id;
    util.post(app.api.updatePaystatus, {
      openid: app.globalData.openid,
      status: 5,
      payStatus:-1,
      orderid: orderId,
      formid:formId
    }, function (res) {
      if (res.code == "200") {
        // 退款已提交
        wx.showToast({
          title: '退款已提交',
          icon: 'success',
          duration: 2000
        })
        that.resetOrderedInfo();
      }
    });
  },
    //确认完成该订单
  comfirmTap: function (event) {
    var that = this;
    var formId = event.detail.formId;
    var orderId = event.currentTarget.dataset.id;
    wx.showModal({
      title: '完成订单',
      content: '请确认该订单已完成',
      cancelText: '取消',
      cancelColor: '#999',
      confirmText: '确认',
      confirmColor: '#4990E2',
      success: function (res) {
        if (res.confirm) {
          app.loading();
          util.post(app.api.updateOrder, { formid: formId, orderid: orderId, status:3 }, function (res) {
            if (res.code == "200") {
              // 订单完成
              wx.showToast({
                title: '订单已完成',
                icon: 'success',
                duration: 2000
              })
              that.resetOrderingInfo();
              that.resetOrderAssessInfo();
              wx.navigateTo({
                url: "/pages/addCommen/addCommen?orderId=" + orderId,
              })
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //取消订单
  cancelTap: function (event) {
    var that = this;
    var formId = event.detail.formId;
    var orderId = event.currentTarget.dataset.id;
    console.log("取消订单OrderId"+orderId);
    console.log("取消订单formId"+formId);
    wx.showModal({
      title: '取消订单',
      content: '请确认要取消该预约服务的订单？',
      cancelText: '取消',
      cancelColor: '#999',
      confirmText: '确认',
      confirmColor: '#4990E2',
      success: function (res) {
        if (res.confirm) {
          app.loading();
          util.post(app.api.updateOrder, { status: 4, orderid: orderId,formid:formId },function(res){
            if(res.code=="200"){
              // 取消订单成功
              wx.showToast({
                title: '取消成功已退款',
                icon: 'success',
                duration: 2000
              })  
              that.resetUnOrder();
            }else{

            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //写评论
  evaluateTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    console.log("订单详情id获取=" + id)
    wx.navigateTo({
      url: "/pages/addCommen/addCommen?orderId="+id,
    })
  },
  //拨打号码
  callphone: function (event) {
    var that=this;
    var orderId = event.currentTarget.dataset.id; // 当前id
    util.post(app.api.callPhone, { openid: app.globalData.openid, orderId: orderId }, function (res) {
      if (res.code == "200") {
        console.log("BIND AXB FUNCTION");
      }
    });
    wx.makePhoneCall({
      phoneNumber: that.data.replyPhone,
      success: function () {
        console.log("成功拨打电话")
      },
    })

  }, 
  PriceDiffTap: function (event) {
    var orderId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my-ordetail/my-ordetail?orderId=' + orderId,
    })
  },

  setCont: function (e) {
    this.setData({
      cont: e.detail.value
    });
  },
  displayApplyTap: function (event){
    var index = event.currentTarget.dataset.index;
    var that = this;
    var hideList = that.data.orderedInfoHide;
    if (hideList[index]){
      hideList[index]=false;
    }else{
      hideList[index] = true;
    }
    that.setData({
      showSafe: (!that.data.showSafe),
      orderedInfoHide: hideList
    })
  },
  safedTap: function (event) {
    var safedId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../assess/assess?id=' + safedId,
    })
  },
  viewDetail:function(event){
    var orderId= event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my-ordetail/my-ordetail?orderId=' + orderId,
    })
  }

})